<?php
/**
 * Created by PhpStorm.
 * User: lili
 * Date: 19-1-20
 * Time: 下午4:56
 */


function pdoms($num){

    $dbms='mysql';     //数据库类型
    $host='localhost'; //数据库主机名
    $dbName='redis';    //使用的数据库
    $user='root';      //数据库连接用户名
    $pass='123456';          //对应的密码
    $dsn="$dbms:host=$host;dbname=$dbName";
    try {
        $dbh = new PDO($dsn, $user, $pass); //初始化一个PDO对象
        echo "连接成功<br/>";

        $res = $dbh->query(' SELECT * from goods where id = 1')->fetchAll();
//    var_dump($res);
        $leftnum = $res[0]['num'];
        if (!file_exists('./log.txt')){
            touch('./log.txt');
        }

        chmod('./log.txt',777);

        file_put_contents('log.txt',"t{$num}:{$leftnum}\n",FILE_APPEND);

        if($leftnum>0){
            $dbh->query('update goods set num=num-1 where id = 1') ;
            /*你还可以进行一次搜索操作 */
            foreach ($dbh->query('SELECT * from goods') as $row) {
                var_dump($row['num']); //你可以用 echo($GLOBAL); 来看到这些值
            }

            $dbh = null;
        }


    } catch (PDOException $e) {
        die ("Error!: " . $e->getMessage() . "<br/>");
    }
}

